package MtG.server;

import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.logging.Level;

import MtG.publicClass.Game;
import MtG.server.components.ActiveSession;
import MtG.server.components.BroadCaster;
import MtG.server.components.Communication;
import MtG.server.components.ErrorLogger;
import MtG.server.components.GameMoves;

/**
 * ServerRun.java is the class that launches the game server and keeps it working. 
 * @author Marek Sirp
 * @author Jaan Sepp
 */
public class ServerRun {
	

	/**
	 * Socket that this server will be using.
	 */
	private static final int PORT = 8899;
	/**
	 * Holds the reference to the game that is currently active on this server.
	 */
	public static Game game = new Game();

	public static void main(String[] args) throws IOException {
		
		
		ActiveSession activeSession = new ActiveSession();
		GameMoves gameMove = new GameMoves();
		ServerSocket serv = new ServerSocket(PORT);
		System.out.println("Server started");
		new BroadCaster(activeSession, gameMove);
		ErrorLogger logger = new ErrorLogger();
		
		try {
			while (true) {
				Socket sock = serv.accept();
				try {
					new Communication(sock, gameMove, activeSession, game, logger);
				} catch (IOException e) {
					logger.login("Failed to connect to game", Level.WARNING);
					sock.close();
				}
			} 
		} finally {
			serv.close();
		}		
	}


}


